﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Controls;
using CCF20Model;
using System.Collections;
using System.Data;
using WiseLink.RDS.Resource;
namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for VehicleMessage.xaml
    /// </summary>
    public partial class ManagerQuery
    {
        //VehicleModel vehicleModel;
        private int CurrentVehicleID = 0;
        
        
        public ManagerQuery()
        {
            InitializeComponent();
            
        }

        //private void RadmaintenanceCount_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    var row = this.radGridTroubleVehicle.ItemContainerGenerator.ContainerFromItem(this.radGridTroubleVehicle.SelectedItem) as GridViewRow;
        //    if (row != null)
        //    {
        //        if (row.DetailsVisibility == Visibility.Visible)
        //        {
        //            row.DetailsVisibility = Visibility.Collapsed;
        //        }
        //        else 
        //        {
        //            row.DetailsVisibility = Visibility.Visible;
        //        }
        //    }

        //}

        private void VehicleMessageToolBar_Click(object sender, RoutedEventArgs e)
        {
            string strAction = ((Button)sender).Name;
            if (strAction == "Refresh")
            {
                LoadData();
                return;
            }

        }
        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
            ApplyResourceToColumn(this.gridFault);
            ApplyResourceToColumn(this.gridContactHistory);
            ApplyResourceToColumn(this.gridCrash);
            ApplyResourceToColumn(this.gridMaintenanceHistory);
            ApplyResourceToColumn(this.gridMaintenanceRecord);
            ApplyResourceToColumn(this.gridRepairHistory);
            ApplyResourceToColumn(this.gridSMSHistory);
            ApplyResourceToColumn(this.gridVehicle);
            ApplyResourceToButton();
            ApplyResourceToTabTitle(this.myTab);

        }

        private void gridVehicle_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (e.AddedItems.FirstOrDefault() != null)
            {
                this.gridVehicle.CurrentItem = e.AddedItems.First();
                VehicleData vd = (VehicleData)this.gridVehicle.CurrentItem;
                CurrentVehicleID = vd.MyVehicle.VehicleID;
                if (this.myTab.SelectedItem != null)
                {
                    TabItem currentTab = (TabItem)this.myTab.SelectedItem;
                    RefreshTab(((TextBlock)currentTab.Header).Text);
                }
            }
            else
            {
                this.gridFault.ItemsSource = null;
                this.gridMaintenanceRecord.ItemsSource = null;
                this.gridContactHistory.ItemsSource = null;
                this.gridSMSHistory.ItemsSource = null;
                this.gridMaintenanceHistory.ItemsSource = null;
                
            }
           
         
        }
        private void LoadData()
        {
            //vehicleModel = (VehicleModel) ModelFactory.CreateModel(ModelName.VehicleModel);
            IList<VehicleData> mySource = vehicleModel.GetAllVehicles(false).ToList();
            this.gridVehicle.ItemsSource = mySource;
        }
        private void RadMenuItem_Click(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
            GridViewRow row = ((ContextMenu)sender).PlacementTarget as GridViewRow;
            if (row != null)
            {
                row.IsSelected = true;
            }
            string temp = row.Cells[0].Content.ToString();
        }

        private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu) sender;
            GridViewRow row = menu.GetClickedElement<GridViewRow>();

            if (row != null)
            {
                row.IsSelected = row.IsCurrent = true;
                
            }
            else
            {
                menu.IsOpen = false;
            }
        }
        private void RadTabControl_SelectionChanged(object sender, RoutedEventArgs e)
        {
             //RadSelectionChangedEventArgs selectionArgs = ( RadSelectionChangedEventArgs )e;
            SelectionChangedEventArgs selectionArgs = (SelectionChangedEventArgs)e;
            string SelectedTabHeader = ((TextBlock)((TabItem)selectionArgs.AddedItems[0]).Header).Text;
            RefreshTab(SelectedTabHeader);
            
        }

        private void RefreshTab(string SelectedTabHeader)
        {
            
            if (SelectedTabHeader.Contains(ButtonString.lblCrashDetail))
            {
                this.gridCrash.ItemsSource = vehicleModel.GetCrashByVehicle(CurrentVehicleID,false);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridCrash.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblFaultDetail))
            {
                this.gridFault.ItemsSource = vehicleModel.GetAllFaultByVehicle(CurrentVehicleID);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridFault.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblCurrentMaintenanceType))
            {
                this.gridMaintenanceRecord.ItemsSource = vehicleModel.GetVehicleMaintenanceRecordByVehicle(CurrentVehicleID);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridMaintenanceRecord.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblMaintenanceHistoryDetail))
            {
                this.gridMaintenanceHistory.ItemsSource = vehicleModel.GetVehicleMaintenanceHistoryByVehicle(CurrentVehicleID);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridMaintenanceHistory.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblShopHistoryDetail))
            {
                this.gridRepairHistory.ItemsSource = vehicleModel.GetRepairHistoryByVehicle(CurrentVehicleID,false);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridRepairHistory.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblSMSHistoryDetail))
            {
                this.gridSMSHistory.ItemsSource = vehicleModel.GetSMSHistoryByVehicle(CurrentVehicleID, false);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridSMSHistory.Rebind()));
            }else if (SelectedTabHeader.Contains(ButtonString.lblContactHistoryDetail))
            {
                this.gridContactHistory.ItemsSource = vehicleModel.GetContactHistoryByVehicle(CurrentVehicleID, false);
                this.Dispatcher.BeginInvoke(new Action(() => this.gridContactHistory.Rebind()));
                }
            

        }
        
    }
}
